
# set config to debug, release
CONFIG = debug

# file with main function
ENTRY_POINT = 

# custom compiler declarations
CUSTOM_FLAGS = #-D VIRG_DEBUG #-D VIRG_NOPINNED #-D VIRG_DEBUG_SLOTS

GCC = /usr/bin/gcc-4.4
GPP = /usr/bin/g++-4.4
CUDA = /usr/local/cuda
NVCC = $(CUDA)/bin/nvcc
CUDA_INCLUDE = $(CUDA)/include
CUDA_LIBRARY = $(CUDA)/lib64
GTEST = ../gtest

FLAGS = -Wall -Wextra -std=c99

INCLUDE_FLAGS = -I $(CUDA_INCLUDE) -I . -I sql/
LINK_FLAGS = -lm -lpthread -L$(CUDA_LIBRARY) -lcuda -lcudart
DEBUG_FLAGS = -g3
RELEASE_FLAGS = -O2
FLAG = $(FLAGS) $(CUSTOM_FLAGS) $(INCLUDE_FLAGS)
CPPFLAG = -Wall $(CUSTOM_FLAGS) $(INCLUDE_FLAGS)

ifeq ($(CONFIG),debug)
	OF = debug
	CC = $(GCC)
	COMPILE_FLAGS = $(FLAG) $(DEBUG_FLAGS)
	NVCC_FLAGS = -g -G -deviceemu
endif
ifeq ($(CONFIG),release)
	OF = release
	CC = $(GCC)
	COMPILE_FLAGS = $(FLAG) $(RELEASE_FLAGS)
	NVCC_FLAGS = -O2
endif

SPECIALS = opcodelist.c sql/lex.yy.c sql/sql.tab.c
CFILES = $(filter-out $(SPECIALS),$(wildcard *.c) $(wildcard */*.c) $(ENTRY_POINT)) $(SPECIALS)
OFILES = $(patsubst %.c,../$(OF)/%.o,$(CFILES))
LFILES = $(OFILES) ../$(OF)/vm/virginia-single.o ../$(OF)/vm/vm_gpu.o

TESTCFILES = $(wildcard test/*.cc)
TESTOFILES = $(patsubst %.cc,../$(OF)/%.o,$(TESTCFILES))

virginian: ../lib/virginian.a $(ENTRY_POINT) test ../$(OF)/virginiantest ../db/comparedb
	cd ../$(OF) && ./virginiantest
	#cd ../$(OF) && valgrind --leak-check=full --suppressions=/home/bakks/virginian/virginian/src/cuda.supp ./virginiantest
#  --gen-suppressions=yes

clean:
	rm -rf ../debug/* ../release/* opcodelist.c vm_gpu.ptx sql/lex.yy.c sql/sql.tab.*
	rm -rf ../lib/virginian.*
	mkdir -p ../debug ../release/
	ls -d */ | sed 's/[a-z]*\//..\/debug\/\0/' | xargs mkdir
	ls -d */ | sed 's/[a-z]*\//..\/release\/\0/' | xargs mkdir

count:
	wc -l *.c *.h */*.c */*.cu */*.h */*.cc */*.y */*.l


$(OFILES): ../$(OF)/%.o: %.c virginian.h
	$(CC) $(COMPILE_FLAGS) -c $< -o $@

../$(OF)/vm/virginia-single.o: vm/virginia.c virginian.h
	$(CC) $(COMPILE_FLAGS) -D __SINGLE -c $< -o $@

../$(OF)/vm/vm_gpu.o: vm/vm_gpu.cu virginian.h
	#$(NVCC) $(CUSTOM_FLAGS) $(INCLUDE_FLAGS) $(NVCC_FLAGS) -arch=sm_21 -c $< -o $@
	$(NVCC) $(CUSTOM_FLAGS) $(INCLUDE_FLAGS) $(NVCC_FLAGS) -gencode arch=compute_20,code=sm_21 -c $< -o $@

opcodelist.c: opcodelist.awk virginian.h
	./opcodelist.awk virginian.h > opcodelist.c

../$(OF)/virginian: $(LFILES)
	$(CC) $(LFILES) $(LINK_FLAGS) -o ../$(OF)/virginian

../lib/virginian.a: $(LFILES)
	ar rvs ../lib/virginian.a $(LFILES)
	cp virginian.h ../lib

../db/comparedb:
	make -s -C ../db clean
	make -s -C ../db
	../db/generate ../db/comparedb 8000000
	../db/generate ../db/comparedb2 3500

ptx: vm/vm_gpu.cu
	#$(NVCC) $(CUSTOM_FLAGS) $(INCLUDE_FLAGS) $(NVCC_FLAGS) --ptxas-options="-v" -arch=sm_21 -g -ptx $< -o vm_gpu.ptx
	$(NVCC) $(CUSTOM_FLAGS) $(INCLUDE_FLAGS) $(NVCC_FLAGS) --ptxas-options="-v" -gencode arch=compute_20,code=sm_21 -g -ptx $< -o vm_gpu.ptx

$(TESTOFILES): ../$(OF)/%.o: %.cc virginian.h test/test.h
	$(GPP) -g3 -I../lib $(CPPFLAG) -c $< -o $@

../$(OF)/virginiantest: $(LFILES) $(TESTOFILES) ../lib/virginian.a
	$(GPP) $(LINK_FLAGS) $(TESTOFILES) ../lib/virginian.a ../lib/libgtest.a -lstdc++ -pthread -L../lib -lgsl -lgslcblas -o ../$(OF)/virginiantest

sql/sql.tab.c: sql/sql.y sql/node.h
	bison --defines=sql/sql.tab.h -o sql/sql.tab.c sql/sql.y

sql/sql.tab.h: sql/sql.y sql/node.h
	bison --defines=sql/sql.tab.h -o sql/sql.tab.c sql/sql.y

sql/lex.yy.c: sql/sql.l sql/sql.tab.h sql/node.h
	flex -o sql/lex.yy.c sql/sql.l





.PHONY: virginian
.PHONY: clean
.PHONY: count

